package com.weishu.housing.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weishu.common.core.utils.poi.ExcelUtil;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.common.core.web.page.TableDataInfo;
import com.weishu.common.log.annotation.Log;
import com.weishu.common.log.enums.BusinessType;
import com.weishu.common.security.annotation.PreAuthorize;
import com.weishu.core.entity.*;
import com.weishu.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 楼盘controller层
 */
@RestController
@RequestMapping("/info")
public class PremisesInfoController {

    @Autowired
    private ITbPremisesInfoService iTbPremisesInfoService;

    @Autowired
    private ISysRegionService regionService;

    @Autowired
    private ITbSubwayService subwayService;

    @Autowired
    private ITbBusinessCircleService businessCircleService;

    @Autowired
    private ITbConfigPremisesPropertyService premisesPropertyService;

    @Autowired
    private ITbRelationPremisesPropertyService relationPremisesPropertyService;

    @Autowired
    private ITbPremisesFacilityService premisesFacilityService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ITbAlbumService tbAlbumService;

    /**
     * 查询楼盘信息列表
     */
    @PreAuthorize(hasPermi = "housing:info:list")
    @GetMapping("/list")
    public AjaxResult selectPremisesInfo(TbPremisesInfo tbPremisesInfo,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<TbPremisesInfo> list = iTbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        System.err.println(list);
        PageInfo<TbPremisesInfo> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }

    /**
     * 省
     */
    @PreAuthorize(hasPermi = "system:info:getsheng")
    @GetMapping("getsheng")
    public List<SysRegion> getsheng(){
        List<SysRegion> getsheng = regionService.getsheng();
        for (SysRegion sysRegion : getsheng) {
            System.out.println(sysRegion);
        }
        return getsheng;
    }

    /**
     * 市
     * @param tbPremisesInfo  楼盘信息
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getshi")
    @GetMapping("getshi")
    public List<SysRegion> getshi(TbPremisesInfo tbPremisesInfo){

        return regionService.getshi(tbPremisesInfo.getProvince());
    }

    /**
     * 县
     * @param tbPremisesInfo 楼盘信息
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getxian")
    @GetMapping("getxian")
    public List<SysRegion> getxian(TbPremisesInfo tbPremisesInfo){
        return regionService.getshi(tbPremisesInfo.getCity());
    }
    /**
     * 删除楼盘信息
     */
    @PreAuthorize(hasPermi = "system:info:remove")
    @Log(title = "楼盘信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(iTbPremisesInfoService.deleteTbPremisesInfoByIds(ids));
    }

    /**
     * 获取地铁
     * @param tbPremisesInfo 楼盘信息
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getsubway")
    @GetMapping("getsubway")
    public List<TbSubway> getsubway(TbPremisesInfo tbPremisesInfo){
        return subwayService.getsubway(tbPremisesInfo.getCity());
    }

    /**
     * 获取直辖市地铁
     * @param tbPremisesInfo 楼盘信息
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getzhixiasubway")
    @GetMapping("getzhixiasubway")
    public List<TbSubway> getzhixiasubway(TbPremisesInfo tbPremisesInfo){
        return subwayService.getsubway(tbPremisesInfo.getProvince());
    }

    /**
     * 获取商圈
     * @param tbPremisesInfo 楼盘信息
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getarea")
    @GetMapping("getarea")
    public List<TbBusinessCircle> getarea(TbPremisesInfo tbPremisesInfo){
        return businessCircleService.getarea(tbPremisesInfo.getCounty());
    }

    /**
     * 获取楼盘特色
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getproperty")
    @GetMapping("getproperty")
    public List<TbBusinessCircle> getproperty(){
        return premisesPropertyService.getproperty();
    }

    /**
     * 生成uuid
     * @return
     */
    @PreAuthorize(hasPermi = "system:info:getuuid")
    @GetMapping("getuuid")
    public String getuuid(){
        return  UUID.randomUUID()+"";
    }

    /**
     * 新增楼盘信息
     */
    @PreAuthorize(hasPermi = "system:info:addloupan")
    @Log(title = "楼盘信息", businessType = BusinessType.INSERT)
    @PostMapping("addloupan")
    public int add(@RequestBody TbPremisesInfo tbPremisesInfo) {
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(tbPremisesInfo.getUuid(), "", 1, TimeUnit.MINUTES);
        if (!aBoolean){
            return 3;
        }else {
            System.out.println("添加楼盘form表单:"+tbPremisesInfo.toString());
            int i = iTbPremisesInfoService.insertTbPremisesInfo(tbPremisesInfo);
            System.out.println("主键id:"+tbPremisesInfo.getId());
            for (Long property : tbPremisesInfo.getPropertys()) {
                TbRelationPremisesProperty tbRelationPremisesProperty = new TbRelationPremisesProperty();
                tbRelationPremisesProperty.setPropertyId(property);
                tbRelationPremisesProperty.setPremisesId(tbPremisesInfo.getId());
                relationPremisesPropertyService.insertTbRelationPremisesProperty(tbRelationPremisesProperty);
                System.out.println("特色ok:"+tbRelationPremisesProperty.toString());
            }
            for (TbPremisesFacility tbPremisesFacility : tbPremisesInfo.getJiaotonglist()) {
                tbPremisesFacility.setPremisesId(tbPremisesInfo.getId());
                tbPremisesFacility.setType(1);
                premisesFacilityService.insertTbPremisesFacility(tbPremisesFacility);
                System.out.println("交通ok:"+tbPremisesFacility.toString());
            }
            for (TbPremisesFacility tbPremisesFacility : tbPremisesInfo.getZhoubianlist()) {
                tbPremisesFacility.setPremisesId(tbPremisesInfo.getId());
                tbPremisesFacility.setType(0);
                premisesFacilityService.insertTbPremisesFacility(tbPremisesFacility);
                System.out.println("周边ok:"+tbPremisesFacility.toString());
            }
            if (tbPremisesInfo.getField1()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField1());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(1);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);

            }
            if (tbPremisesInfo.getField2()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField2());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(2);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            if (tbPremisesInfo.getField3()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField3());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(3);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            if (tbPremisesInfo.getField4()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField4());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(4);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            if (tbPremisesInfo.getField5()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField5());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(5);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            return 1;
        }

    }


}
